﻿using ERPPublic;
using FinancialManagement.Application.Command;
using FinancialManagement.Application.Command.SaleContractCommand;
using FinancialManagement.Application.Command.SalesReturnCommand;
using FinancialManagement.Application.CommandHandler.SaleContractCommandHandlers;
using FinancialManagement.Domain.Dto.SaleContract;
using FinancialManagement.Domain.DTO;
using FinancialManagement.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace FinancialManagement.Controllers
{
    /// <summary>
    /// 销售合同
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class SaleContractController : ControllerBase
    {
        protected readonly IMediator mediatorp;
        public SaleContractController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }

        /// <summary>
        /// 销售合同添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddStateSalesReturn( SaleContractAddCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.AddDtos.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddDtos.AddName = name;
                command.AddDtos.AddTime = DateTime.Now;
                command.AddDtos.SCSalespeople = name;
               //获取用户ID
               var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.AddDtos.User_Id = int.Parse(userId);
                result.Code = 200;
                result.Message = "添加销售合同成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 销售合同显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<SaleContractShowDto>>> ShowStateSalesReturn([FromQuery]SaleContractShowCommand command)
        {
            ApiResult<PageDto<SaleContractShowDto>> result = new ApiResult<PageDto<SaleContractShowDto>>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }




        /// <summary>
        /// 销售合同反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SaleContractShowDto>> BlikStateSalesReturn([FromQuery] SaleContractBlikCommand command)
        {
            ApiResult<SaleContractShowDto> result = new ApiResult<SaleContractShowDto>();
            try
            {
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }


        /// <summary>
        /// 销售合同修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdStateSalesReturn(SaleContractUpdCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.AddDtos.UpdName = name;
                command.AddDtos.UpdTime = DateTime.Now;
                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }




        /// <summary>
        /// 销售合同删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelStateSalesReturn(SaleContractDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.DelName = name;
                result.Code = 200;
                result.Message = "删除成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 获取销售利润列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<SaleProfitDTO>>> GetSaleProfitList([FromQuery]SaleProfitCommand command)
        {
            ApiResult<PageDto<SaleProfitDTO>> result = new ApiResult<PageDto<SaleProfitDTO>>();
            try
            {

                result.Data = await mediatorp.Send(command);
                result.Code = 200;
                result.Message = "获取成功！";
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
